package com.ruoyi.project.system.controller;

import java.util.List;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.security.LoginBody;
import com.ruoyi.framework.security.LoginUser;
import com.ruoyi.framework.security.service.SysLoginService;
import com.ruoyi.framework.security.service.SysPermissionService;
import com.ruoyi.framework.security.service.TokenService;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.project.system.domain.SysMenu;
import com.ruoyi.project.system.domain.SysUser;
import com.ruoyi.project.system.service.ISysMenuService;

/**
 * 登录验证
 * 
 * @author ruoyi
 */
@RestController
public class SysLoginController
{
    @Autowired
    private SysLoginService loginService;

    @Autowired
    private ISysMenuService menuService;

    @Autowired
    private SysPermissionService permissionService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录方法
     * 
//     * @param username 用户名
//     * @param password 密码
//     * @param captcha 验证码
//     * @param uuid 唯一标识
     * @return 结果
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody LoginBody loginBody)
    {
        System.out.println("--------------------login");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
        String token = loginService.login(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
                loginBody.getUuid());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * 获取用户信息
     * 
     * @return 用户信息
     */
    @GetMapping("getInfo")
    public AjaxResult getInfo()
    {
        System.out.println("--------------------getInfo");
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        LoginUser loginUser = tokenService.getLoginUserByLongToToken();

        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }

    /**
     * 获取路由信息
     * 
     * @return 路由信息
     */
    @GetMapping("getRouters")
    public AjaxResult getRouters()
    {
        System.out.println("--------------------getRouters");
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }

    /*-----------------接口改造，APP,CRM端登录、获取用户信息、获取路由信息------------------*/

    /**
     *crm端登录
     * @param loginBody
     * @return
     */
    @PostMapping("/logincrm")
//    @CrossOrigin //解决跨域问题
    public AjaxResult logincrm(@RequestBody LoginBody loginBody)
    {
        System.out.println("--------------------logincrm");
        AjaxResult ajax = AjaxResult.success();
        // 生成令牌
//        String token = loginService.logincrm(loginBody.getUsername(), loginBody.getPassword(), loginBody.getCode(),
//                loginBody.getUuid());
        String token = loginService.logincrm(loginBody.getUsername(), loginBody.getPassword());
        ajax.put(Constants.TOKEN, token);
        return ajax;
    }

    /**
     * crm获取用户信息
     *
     * @return 用户信息
     */
    @GetMapping("getInfocrm")
//    @CrossOrigin //解决跨域问题
    public AjaxResult getInfocrm()
    {

        System.out.println("--------------------getInfocrm");
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
//        LoginUser loginUser = tokenService.getLoginUserByLongToToken();

        SysUser user = loginUser.getUser();
        // 角色集合
        Set<String> roles = permissionService.getRolePermission(user);
        // 权限集合
        Set<String> permissions = permissionService.getMenuPermission(user);
        AjaxResult ajax = AjaxResult.success();
        ajax.put("user", user);
        ajax.put("roles", roles);
        ajax.put("permissions", permissions);
        return ajax;
    }


    /**
     * crm获取路由信息
     * @return 路由信息
     */
    @GetMapping("getRouterscrm")
//    @CrossOrigin //解决跨域问题
    public AjaxResult getRouterscrm()
    {

        System.out.println("--------------------getRouterscrm");
        LoginUser loginUser = tokenService.getLoginUserByLongToToken(ServletUtils.getRequest());

        // 用户信息
        SysUser user = loginUser.getUser();
        List<SysMenu> menus = menuService.selectMenuTreeByUserId(user.getUserId());
        return AjaxResult.success(menuService.buildMenus(menus));
    }







}
